package com.afkt.project.ui.adapter;

import com.afkt.project.model.bean.ItemStickyBean;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseTextView;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * detail: 吸附 Item 预览 View Adapter
 *
 * @author Ttt
 */
public class ItemStickyAdapter extends BaseItemProvider {

    private static final String TAG = "ItemStickyAdapter";
    private List<ItemStickyBean> lists = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private Context context;

    public ItemStickyAdapter(List<ItemStickyBean> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    public void setTitles(List<String> titles) {
        this.titles = titles;
        LogUtils.error(TAG,titles.size()+"");
    }

    @Override
    public int getCount() {
        return lists.size() + titles.size();
    }

    @Override
    public Object getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemComponentType(int position) {
        if (position == 0 || position == 4 || position == 8) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getComponentTypeCount() {
        return 2;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        if (component == null) {
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_adapter_item_sticky, null, false);
            viewHolder = new ViewHolder();
            viewHolder.vidAisTitleTv = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ais_title_tv);
            viewHolder.vidAisTimeTv = (BaseTextView) component.findComponentById(ResourceTable.Id_vid_ais_time_tv);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        viewHolder.vidAisTitleTv.setText(lists.get(i).title);
        viewHolder.vidAisTimeTv.setText(lists.get(i).timeFormat);
        return component;
    }

    static class ViewHolder {
        BaseTextView vidAisTitleTv;
        BaseTextView vidAisTimeTv;
    }
}